# coding=utf-8
from rectpack import *


AlgoList = ['MaxRectsBl',
            # 'MaxRectsBssf',
            # 'MaxRectsBaf',
            # 'MaxRectsBlsf',
            # 'SkylineBl',
            # 'SkylineBlWm',
            # 'SkylineMwf',
            # 'SkylineMwfl',
            # 'SkylineMwfWm',
            # 'SkylineMwflWm',
            # 'GuillotineBssfSas',
            # 'GuillotineBssfLas',
            # 'GuillotineBssfSlas',
            # 'GuillotineBssfLlas',
            # 'GuillotineBssfMaxas',
            # 'GuillotineBssfMinas',
            # 'GuillotineBlsfSas',
            # 'GuillotineBlsfLas',
            # 'GuillotineBlsfSlas',
            # 'GuillotineBlsfLlas',
            # 'GuillotineBlsfMaxas',
            # 'GuillotineBlsfMinas',
            # 'GuillotineBafSas',
            # 'GuillotineBafLas',
            # 'GuillotineBafSlas',
            # 'GuillotineBafLlas',
            # 'GuillotineBafMaxas',
            # 'GuillotineBafMinas'
            ]
def go_rectpack():
    chicun = 127

    rectangles = [
        (123, 123),
        (123, 123)
    ]

    tumianji = 0
    xmaxnum = 0
    xh = 0
    liyonglv = 0
    packers = newPacker()
    # 创建装箱器，设置装箱区域大小为1000x1000
    # 循环算法
    print("测试")
    for i in AlgoList:
        maxnum = 0
        h = 0
        tumianji = 0
        # 按钮初始化
        packer = newPacker(pack_algo=globals()[i])
        packer.add_bin(chicun, 5000)

        # 添加待打包的矩形
        for r in rectangles:
            # print(r)
            packer.add_rect(*r)
            # print(r)
        # 运行装箱算法，获取打包结果
        packer.pack()

        if len(rectangles) > len(packer.rect_list()):
            continue
        print("排列成功数量", len(packer.rect_list()), "算法", i)
        # print(packer.rect_list())
        # 循环单个算法的返回结果也就是
        # 打印装箱结果
        for b in packer:

            for r in b:
                tumianji += int(int(r.width) * int(r.height))

                if r.y + r.height > maxnum:
                    maxnum = r.y + r.height
                    jieguo = (tumianji / (maxnum * chicun)) * 100
                    print("当前算法结果：", jieguo)
                    if jieguo > liyonglv:
                        print("当前算法最优结果：", jieguo)
                        liyonglv = jieguo
                        packers = packer

        print("图面积..", tumianji)
        print("利用率..", liyonglv)
        print("使用画布高度", str(maxnum))
        print("==================================")

    for item in packers:
        print(item)
        for r in item:
            print(r)
    # self.return_msg_TextBrowser = QTextBrowser(self)
    # self.return_msg_TextBrowser.setGeometry(0, maxnum+h, self.chicun, 50)
    # self.return_msg_TextBrowser.setFontPointSize(10)
    # self.return_msg_TextBrowser.setText(self._translate("Example", "利用率："))
    # self.return_msg_TextBrowser.append(str(((self.tumianji/((maxnum+h)*self.chicun)))*100))

    # print(str(r.x)+"   {r}")
    # print(self.tumianji)

    # print(str(maxnum+h))
    # print(str(round(jieguo,2))+"..."+i)

    # 输出未能放入的矩形
    # print("Not packed rectangles:", packer.rect_list())


if __name__ == "__main__":
    go_rectpack()
